---
--- 插件入口handler
--- Created by E.T.
--- DateTime: 2018/10/25 11:35
---
local BasePlugin = require "kong.plugins.base_plugin"
local limit = require "kong.plugins.xhg-rate-limiting.limit.access_flow_control"
require "kong.plugins.xhg-rate-limiting.limit.conn_limit"
local LimitHandler = BasePlugin:extend()
local ngx_log = ngx.log
local DEBUG = ngx.NOTICE
LimitHandler.PRIORITY = 1001
LimitHandler.VERSION = "1.0.0"

function LimitHandler:new()
    LimitHandler.super.new(self, "xhg-rate-limiting")
end

function LimitHandler:init_worker()
    --ngx_log(DEBUG, "executing plugin \"", self._name, "\": init_worker")
end

function LimitHandler:rewrite(conf)
    --ngx_log(DEBUG, "executing plugin \"", self._name, "\": rewrite")
end

function LimitHandler:access(conf)
    LimitHandler.super.access(self)
    kong.ctx.shared.config = conf
    limit.execute()
    --ngx_log(DEBUG, "access \"", self._name, "\": access")
end

--function AuthHandler:header_filter()
    --ngx_log(DEBUG, "上游状态",kong.service.response.get_status())
    --ngx_log(DEBUG, "下游状态",kong.response.get_status())
    --ngx_log(DEBUG, "来源",kong.response.get_source())
    --ngx_log(DEBUG, "executing plugin \"", self._name, "\": header_filter")
--end
--function AuthHandler:body_filter()
--  ngx_log(DEBUG, "executing plugin \"", self._name, "\": body_filter")
--end

function LimitHandler:log()
    --释放接口连接，更新限流速率
    ConnLimit:leaving()
end

return LimitHandler